﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.UI.common;

namespace rock.UI.fee
{
    public partial class FormFeeShaoOtherMoney : Form
    {
        DataSet typeds;
        ModelFEE_OTHER aModelFEE_OTHER = new ModelFEE_OTHER();
        public ModelFEE_OTHER _ModelFEE_OTHER
        {
            get
            {
                return aModelFEE_OTHER;
            }
            set
            {
                aModelFEE_OTHER = value;
            }
        }


        DalFEE_OTHER aDalFEE_OTHER = new DalFEE_OTHER();
        public FormFeeShaoOtherMoney()
        {
            InitializeComponent();
            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon =    global::rock.Properties.Resources.house;
        }



        private void FormFeeShaoRoom_Load(object sender, EventArgs e)
        {
           
           
            //费用类型
       
            this.comboBox4FeeType.DisplayMember = "Name";
            this.comboBox4FeeType.ValueMember = "IntId";
            this.comboBox4FeeType.DataSource = Constant.FEE_FEE_TYPE_LIST;
            //更新
            //if (this.aDalFEE_OTHER.Exists(this._ModelFEE_OTHER.SEQID))
            //{
            //    List<ComboBoxInfo> names = new List<ComboBoxInfo>();
            //    names.Add(new ComboBoxInfo { Id = this._ModelFEE_OTHER.FEE_NAME, Name = this._ModelFEE_OTHER.FEE_NAME });
            //    this.comboBox3FeeName.DisplayMember = "Name";
            //    this.comboBox3FeeName.ValueMember = "Id";
            //    this.comboBox3FeeName.DataSource = names;
            //}
            //else
            //{
                //费用名称
                DalFEE_TYPE aDalFEE_TYPE = new DalFEE_TYPE();
                this.typeds = aDalFEE_TYPE.GetList("");
                this.comboBox3FeeName.DisplayMember = "NAME";
                this.comboBox3FeeName.ValueMember = "SEQID";
                this.comboBox3FeeName.DataSource = typeds.Tables[0];
            //}

           
            //收费状态
 
            this.comboBox2Status.DisplayMember = "Name";
            this.comboBox2Status.ValueMember = "IntId";
            this.comboBox2Status.DataSource = Constant.FEE_OTHERROOM_STATUS_LIST;
 
            //年下拉框
            List<ComboBoxInfo> years = new List<ComboBoxInfo>();
            for(int i= 2012 ; i<=2042 ; i++){
                years.Add(new ComboBoxInfo { IntId = i, Name = i+"" });
            }
            this.comboBox1Year.DisplayMember = "Name";
            this.comboBox1Year.ValueMember = "IntId";
            this.comboBox1Year.DataSource = years;
  
            //月下拉框
            List<ComboBoxInfo> months = new List<ComboBoxInfo>();
            for (int i = 1; i <= 12; i++)
            {
                string value = i < 10 ? ("0" + i) : (i+"");
                months.Add(new ComboBoxInfo { Id = value, Name = value });
            }
            this.comboBox2Month.DisplayMember = "Name";
            this.comboBox2Month.ValueMember = "Id";
            this.comboBox2Month.DataSource = months;
            


            //更新
            if (this.aDalFEE_OTHER.Exists(this._ModelFEE_OTHER.SEQID))
            {
                String monthstr = this._ModelFEE_OTHER.MONTH ;
                int year = int.Parse(monthstr.Substring(0,4));
                string month =  monthstr.Substring(4,2) ;
                this.comboBox1Year.SelectedValue = year;
                this.comboBox2Month.SelectedValue = month;

                this.comboBox2Status.SelectedValue = this._ModelFEE_OTHER.FEE_STATUS  ;
                this.comboBox4FeeType.SelectedValue = this._ModelFEE_OTHER.FEE_MODEL;
                //特殊处理
               // this.comboBox3FeeName.Text = this._ModelFEE_OTHER.FEE_NAME;
               this.comboBox3FeeName.Text = this._ModelFEE_OTHER.FEE_NAME;
               this.comboBox3FeeName.Enabled = false;

              

                 
                this.userControlImportRentContractInfo1._SHOW_BUTTON = false;
                this.userControlImportRentContractInfo1._CONTRACT_SEQID = this._ModelFEE_OTHER.CONTRACT_SEQID;


                DalRENT_CONTRACT _DalRENT_CONTRACT = new DalRENT_CONTRACT();
                ModelRENT_CONTRACT _ModelRENT_CONTRACT = _DalRENT_CONTRACT.GetModel(this._ModelFEE_OTHER.CONTRACT_SEQID);
                this.userControlImportRentContractInfo1.selectedOk(_ModelRENT_CONTRACT.SEQID, _ModelRENT_CONTRACT.HOUSE_ROOM_SEQID, _ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);

                this.button4AthtoSet.Enabled = false;

                //更新的时候设置原默认的的费用值
                this.textBox2Price.Text = this._ModelFEE_OTHER.FEE + "";
            }
            else
            {

                //如果添加又带合同号.则自动加载
                if (this._ModelFEE_OTHER.CONTRACT_SEQID > 0)
                {
                    this.userControlImportRentContractInfo1._CONTRACT_SEQID = this._ModelFEE_OTHER.CONTRACT_SEQID;
                    DalRENT_CONTRACT _DalRENT_CONTRACT = new DalRENT_CONTRACT();
                    ModelRENT_CONTRACT _ModelRENT_CONTRACT = _DalRENT_CONTRACT.GetModel(this._ModelFEE_OTHER.CONTRACT_SEQID);
                    this.userControlImportRentContractInfo1.selectedOk(_ModelRENT_CONTRACT.SEQID, _ModelRENT_CONTRACT.HOUSE_ROOM_SEQID, _ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);
                }


                String monthstr = this._ModelFEE_OTHER.MONTH;
                int year = DateTime.Now.Year;
                int month = DateTime.Now.Month;
                this.comboBox1Year.SelectedValue = year;
                string value = month < 10 ? ("0" + month) : (month + "");
                this.comboBox2Month.SelectedValue =value ;

                this.comboBox2Status.SelectedValue = Constant.FEE_ROOM_WEISHAO;
              //  this.comboBox4FeeType.SelectedValue = "";
               // this.comboBox3FeeName.SelectedValue = "";
  
 
                this.userControlImportRentContractInfo1._SHOW_BUTTON = true;
                this._ModelFEE_OTHER.OPERA_TIME = DateTime.Now;
                this._ModelFEE_OTHER.RECORD_TIME = DateTime.Now;
                this._ModelFEE_OTHER.START_TIME = DateTime.Now;
                this._ModelFEE_OTHER.STOP_TIME = DateTime.Now;


            }


            if (this._ModelFEE_OTHER.FEE_MODEL == Constant.FEE_TYPE_COUNT)
            {
                this.textBox1Count.Text = this._ModelFEE_OTHER.TOTAL_VALUE + "";
                this.textBox4CountStart.Text = this._ModelFEE_OTHER.START_VALUE + "";
                this.textBox5CountStop.Text = this._ModelFEE_OTHER.STOP_VALUE + "";
                this.textBox4CountStart.Enabled = true;
                this.textBox5CountStop.Enabled = true;
                this.textBox1Count.Enabled = true;
            }
            else
            {
                this.textBox1Count.Enabled = false;
                this.textBox4CountStart.Enabled = false;
                this.textBox5CountStop.Enabled = false;
            }

            if (this._ModelFEE_OTHER.FEE_MODEL == Constant.FEE_TYPE_SIZE)
            {
                //this._ModelFEE_OTHER.
            }

            
            this.textBox1Note.Text = this._ModelFEE_OTHER.NOTE;
            this.textBox3TatleMoney.Text = this._ModelFEE_OTHER.TOTAL_FEE + "";
            this.textBox4Opera.Text = Manager.getInstance().userAccount.USERNAME;
           


            this.dateTimePicker1Start.Text = this._ModelFEE_OTHER.START_TIME.ToString(StringUtils.YYYYMMDD);
            this.dateTimePicker2Stop.Text = this._ModelFEE_OTHER.STOP_TIME.ToString(StringUtils.YYYYMMDD);
            this.dateTimePicker1Opear.Text = this._ModelFEE_OTHER.OPERA_TIME.ToString(StringUtils.YYYYMMDD);
            this.dateTimePicker2Record.Text = this._ModelFEE_OTHER.RECORD_TIME.ToString(StringUtils.YYYYMMDD);
        }

        private Boolean check()
        {
            try
            {//dalRENT_CUSTOM
                if (this.userControlImportRentContractInfo1._CONTRACT_SEQID <= 0)
                {
                    throw new Exception("请先选择合同信息");
                }



                StringUtils.checkText(this.textBox1Note.Text, 0, 300, "备注信息");

                StringUtils.checkIsDecimal(this.textBox3TatleMoney.Text, "费用总金额");


                if ((int)this.comboBox4FeeType.SelectedValue == Constant.FEE_TYPE_COUNT) { 
                    StringUtils.checkIsDecimal(this.textBox5CountStop.Text, "读表起数");
                    StringUtils.checkIsDecimal(this.textBox4CountStart.Text, "读表止数");
                    StringUtils.checkIsDecimal(this.textBox1Count.Text, "本次用量");
                }
               
                int status = (int)this.comboBox2Status.SelectedValue;
                if (status == Constant.FEE_ROOM_BUSHAO)
                {
                    if (Decimal.Parse(textBox3TatleMoney.Text) > 0)
                    {
                        throw new Exception("选择免收款时不能输有费用总金额,请输入0");
                    }
                }
                else 
                {
                    if (Decimal.Parse(textBox3TatleMoney.Text) <= 0)
                    {
                        throw new Exception("请输入费用总金额,或按计算按钮进行计算费用总金额.");
                    }
                }


                             
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }
            this._ModelFEE_OTHER.FEE_NAME = this.comboBox3FeeName.Text;
            this._ModelFEE_OTHER.FEE = Convert.ToDecimal(this.textBox2Price.Text);
            this._ModelFEE_OTHER.TOTAL_FEE = Convert.ToDecimal(this.textBox3TatleMoney.Text);
            this._ModelFEE_OTHER.NOTE = this.textBox1Note.Text;
            this._ModelFEE_OTHER.OPERA_MAN = this.textBox4Opera.Text;
            this._ModelFEE_OTHER.OPERA_TIME = this.dateTimePicker1Opear.Value;
            this._ModelFEE_OTHER.RECORD_TIME = this.dateTimePicker2Record.Value;

            this._ModelFEE_OTHER.FEE_STATUS = Convert.ToInt32(this.comboBox2Status.SelectedValue);
            this._ModelFEE_OTHER.CONTRACT_SEQID = this.userControlImportRentContractInfo1._CONTRACT_SEQID;
            this._ModelFEE_OTHER.CUSTOM_SEQID = this.userControlImportRentContractInfo1._CUSTOM_SEQID;
            this._ModelFEE_OTHER.HOUSE_SEQID = this.userControlImportRentContractInfo1._ROOM_SEQID;


            this._ModelFEE_OTHER.MONTH = this.comboBox1Year.SelectedValue + "" + this.comboBox2Month.SelectedValue;
            this._ModelFEE_OTHER.START_TIME = this.dateTimePicker1Start.Value;
            this._ModelFEE_OTHER.STOP_TIME = this.dateTimePicker2Stop.Value;

            this._ModelFEE_OTHER.FEE_MODEL = (int)this.comboBox4FeeType.SelectedValue;
            if(this._ModelFEE_OTHER.FEE_MODEL == Constant.FEE_TYPE_COUNT){
                this._ModelFEE_OTHER.START_VALUE = Decimal.Parse(this.textBox4CountStart.Text);
                this._ModelFEE_OTHER.STOP_VALUE =  Decimal.Parse(this.textBox5CountStop.Text);
                this._ModelFEE_OTHER.TOTAL_VALUE = Decimal.Parse(this.textBox1Count.Text);
                
            }


            if (aDalFEE_OTHER.Exists(this._ModelFEE_OTHER.SEQID))
            {
                DialogResult dialogResult = MessageBox.Show("请确认是否要更新费用情况", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dialogResult.Equals(DialogResult.OK))
                {
                    //刷新数据
                    aDalFEE_OTHER.Update(this._ModelFEE_OTHER);
                }
                else
                {
                    return;
                }


            }
            else
            {
                aDalFEE_OTHER.Add(this._ModelFEE_OTHER);
            }
            //触发代理
            oneRefreshParentData(1);
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //代理
        public delegate void RefreshParentData(int page);
        public RefreshParentData oneRefreshParentData;






        //更换计费名称.引发不同的计费模式
        private void comboBox3FeeName_SelectedIndexChanged(object sender, EventArgs e)
        {
            //更新不充许重设费用
            if (this.aDalFEE_OTHER.Exists(this._ModelFEE_OTHER.SEQID))
            {
                return;
            }


            int SEQID = 0;
            try
            {
                SEQID = (int)this.comboBox3FeeName.SelectedValue;
                if (SEQID == 0) { 
                    throw new Exception("");
                }
            }
            catch (Exception)
            {
              //  MessageBox.Show("设置名称错");
                return;
            }
          
            ModelFEE_TYPE  aFEE_TYPE =  getModelFEE_TYPE(SEQID);

           if (aFEE_TYPE.FEE_MODEL == Constant.FEE_TYPE_COUNT)
            {
                this.textBox1Count.Enabled = true;
                this.textBox4CountStart.Enabled = true;
                this.textBox5CountStop.Enabled = true;
                this.textBox1Count.Text = this._ModelFEE_OTHER.TOTAL_VALUE + "";
                this.textBox4CountStart.Text = this._ModelFEE_OTHER.START_VALUE + "";
                this.textBox5CountStop.Text = this._ModelFEE_OTHER.STOP_VALUE + "";
                
            }
            else
            {
                this.textBox1Count.Enabled = false;
                this.textBox4CountStart.Enabled = false;
                this.textBox5CountStop.Enabled = false;
            }
           this.textBox2Price.Text = aFEE_TYPE.FEE + "";
           this.comboBox4FeeType.SelectedValue = aFEE_TYPE.FEE_MODEL;
           this.textBox1Count.Text = "";
           this.textBox3TatleMoney.Text = "";
                

            
        }


        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this._ModelFEE_OTHER.FEE = Decimal.Parse(this.textBox2Price.Text);
            //更新
            if (this.aDalFEE_OTHER.Exists(this._ModelFEE_OTHER.SEQID))
            {
                int model = (int)this.comboBox4FeeType.SelectedValue;
                calculater(model, this._ModelFEE_OTHER.FEE);
            }
            else 
            { 
                int model = 0;
                try
                {
                    model = (int)this.comboBox3FeeName.SelectedValue;
                     if (model == 0)
                    {
                        MessageBox.Show("请设置好相关记录", "提示");
                        return;
                    }
                }
                catch (Exception)
                {

                    MessageBox.Show("请设置好相关记录", "提示");
                    return;
                }

                int SEQID = (int)this.comboBox3FeeName.SelectedValue;
                ModelFEE_TYPE aFEE_TYPE = getModelFEE_TYPE(SEQID);
                calculater(aFEE_TYPE.FEE_MODEL, aFEE_TYPE.FEE);
            }

        }



        private void calculater(int model  , Decimal fee ) {

            int status = (int)this.comboBox2Status.SelectedValue;
            if (model == Constant.FEE_TYPE_ANCHI)
            {
               
               
                if (status == Constant.FEE_ROOM_BUSHAO)
                {
                    this.textBox3TatleMoney.Text = 0 + "";
                }
                else 
                {
                    this.textBox3TatleMoney.Text = fee + "";
                }

            }
            else if (model == Constant.FEE_TYPE_SIZE)
            {


                if (this.userControlImportRentContractInfo1._CONTRACT_SEQID <= 0)
                {
                    MessageBox.Show("请先选择合同信息", "提示");
                    return;
                }
                

                if (status == Constant.FEE_ROOM_BUSHAO)
                {
                    this.textBox3TatleMoney.Text = 0 + "";
                }else
                {
                    this.textBox3TatleMoney.Text = (fee * this.userControlImportRentContractInfo1._ROOM_SIZE) + "";
                }
            }
            else if (model == Constant.FEE_TYPE_COUNT)
            {
                decimal start = Decimal.Parse(this.textBox4CountStart.Text);
                decimal stop = Decimal.Parse(this.textBox5CountStop.Text);


                if ( stop <= 0 || stop < start)
                {
                    MessageBox.Show("请先设置用量的起止值", "提示");
                    return;
                }


                this.textBox1Count.Text = (stop - start) + "";

                this._ModelFEE_OTHER.TOTAL_FEE = ((stop - start) * fee);
                this.textBox3TatleMoney.Text = this._ModelFEE_OTHER.TOTAL_FEE+ "";

                if (status == Constant.FEE_ROOM_BUSHAO)
                {
                    this.textBox3TatleMoney.Text = 0 + "";
                }
                else
                {
                    this.textBox3TatleMoney.Text = this._ModelFEE_OTHER.TOTAL_FEE + "";
                }

            }
        }



        private ModelFEE_TYPE getModelFEE_TYPE(int SEQID) {
            DalFEE_TYPE aDalFEE_TYPE = new DalFEE_TYPE();
            ModelFEE_TYPE aFEE_TYPE = aDalFEE_TYPE.GetModel(SEQID);
            return aFEE_TYPE;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.userControlImportRentContractInfo1._CONTRACT_SEQID <= 0)
            {

                MessageBox.Show("请先选择合同信息", "提示");
                return;

            }

            if (((int)comboBox3FeeName.SelectedValue) <= 0)
            {

                MessageBox.Show("请先选择费用名称信息", "提示");
                return;

            }

            int feeTypeSeqid = (int)comboBox3FeeName.SelectedValue;
            ModelFEE_TYPE aFEE_TYPE = getModelFEE_TYPE(feeTypeSeqid);

            DataSet ds = this.aDalFEE_OTHER.getLastRecordByContract(this.userControlImportRentContractInfo1._CONTRACT_SEQID, aFEE_TYPE.NAME);
            //加载年月为当月
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                //没有加载到数据.默认当前.
                MessageBox.Show("此合同还没有完整的交费记录.默认开始日期为当前.结束日期为开始日期加一个月.", "提示");

                DateTime lastDate = DateTime.Now;
                this.dateTimePicker1Start.Value = lastDate;
                DateTime nextDate = lastDate.AddMonths(1);
                this.dateTimePicker2Stop.Value = nextDate;
                return;
                

            }



            //加载上一次最后收租时间

            //  String yyyymm = (String)ds.Tables[0].Rows[0]["YYYYMM"];
            DateTime lastDate2 = (DateTime)ds.Tables[0].Rows[0]["STOP_TIME"];
            this.dateTimePicker1Start.Value = lastDate2;
            DateTime nextDate2 = (DateTime)ds.Tables[0].Rows[0]["NEXT_STOP"];
            this.dateTimePicker2Stop.Value = nextDate2;
            this.textBox4CountStart.Text = (Decimal)ds.Tables[0].Rows[0]["STOP_VALUE"] + "";

            //设置到期时间为开始后一个月
        }

        
    }
}
